/**
 ************************************* Copyright ******************************
 *
 * FileName   : debug.c
 * Version    : v1.0
 * Author     : yyc
 * Date       : 2021-04-14
 * Description:
 * Function List:
 1. ....
 <version>:
 <modify staff>:
 <data>:
 <description>:
 2. ...
 ******************************************************************************
 */
#include "debug.h"
#include "usart.h"
#include <string.h>
#include "user_task.h"
#include "user_queue.h"
#include "sbus.h"

#define PWM_LOW				1000.000
#define PWM_HIGH			2000.000
#define SBUS_CH_LOW			300.000
#define SBUS_CH_HIGH		1700.000

#define FRM_LEN			25
#define CHECK_LEN		4

USART3_RECEIVETYPE UsartType3;
/* -------------------------------- begin  -------------------------------- */
/**
 * @Name    UART_RxIdleCallback6
 * @brief
 * @param   huart: [输入/出]
 * @retval
 * @author  yyc
 * @Data    2021-04-15
 **/
/* -------------------------------- end -------------------------------- */
void UART_RxIdleCallback3(UART_HandleTypeDef *huart)
{
	if ((__HAL_UART_GET_FLAG(huart,UART_FLAG_IDLE) != RESET)) {
		__HAL_UART_CLEAR_IDLEFLAG(&huart3);
		HAL_UART_DMAStop(&huart3);
		UsartType3.RX_Size = RX_LEN3 - huart3.hdmarx->Instance->NDTR;
		/*数据处理，此处入缓存*/

		/*********************************************************************
		 * 两种情况导致dma失能：1.接收溢出 2.在dma关闭期间，串口接收数据
		 * 清除接收溢出标志位，再开启dma进行接收
		 *********************************************************************/
		__HAL_UART_CLEAR_OREFLAG(&huart3);
		HAL_UART_Receive_DMA(&huart3, UsartType3.RX_pData, RX_LEN3);
	}
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if (huart->Instance == USART3)	// 判断是由哪个串口触发的中断
	{
		UsartType3.RX_Size = RX_LEN3;
		osMessageQueuePut(myQueue01Handle, &UsartType3, 0U, 0U);
		HAL_UART_Receive_IT(&huart3, UsartType3.RX_pData, RX_LEN3);
	}
}

uint8_t sbus_Ceartframe(uint8_t *fsh_flag, uint8_t *data)
{
	static uint8_t temp[64] = { 0 };
	static uint8_t cnt = 0;
	USART3_RECEIVETYPE Usart3_rx;
	uint8_t count;
	uint8_t check;
	uint8_t chekc_code[CHECK_LEN] = { 0x04, 0x14, 0x24, 0x34 };
	while (0 != osMessageQueueGetCount(myQueue01Handle)) {
		osMessageQueueGet(myQueue01Handle, &Usart3_rx, 0U, 0U);

		if (Usart3_rx.RX_pData[0] == 0x0f) {
			if ((cnt >= FRM_LEN - 1)) {
				for (uint8_t i = 0; i < cnt + 1; i++) {
					data[i] = temp[i];
					//			HAL_UART_Transmit(&huart2, &data[i], 1, 100);
				}
				count = 0;
				check = 0;
				while ((count < CHECK_LEN) && (check == 0)) {
					if (temp[FRM_LEN - 1] == chekc_code[count]) {
						check = 1;
						*fsh_flag = HAL_OK;
					} else {
						*fsh_flag = HAL_ERROR;
					}
					count++;
				}
				memset(temp, 0x00, sizeof(temp));
				cnt = 0;
				temp[0] = Usart3_rx.RX_pData[0];
			} else {
				cnt++;
				temp[cnt] = Usart3_rx.RX_pData[0];
				*fsh_flag = HAL_ERROR;
			}
		} else {
			*fsh_flag = HAL_BUSY;
			cnt++;
			temp[cnt] = Usart3_rx.RX_pData[0];
			cnt = cnt % 64;
		}
	}
	return 0;
}

uint8_t sbus_Analysis(uint8_t *data, uint16_t cnt, uint16_t *ch)
{
	for (uint8_t i = 0; i < 16; i++)
		ch[i] = 0;
	ch[0] = ((data[2] & 0x07) << 8) + data[1]; //sBus[ 2] low3 + sBus[ 1] low8
	ch[1] = ((data[3] & 0x3F) << 5) + (data[2] >> 3); //sBus[ 3] low6 + sBus[ 2] high5
	ch[2] = ((data[5] & 0x01) << 10) + (data[4] << 2) + (data[3] >> 6); //sBus[ 5] low1 + sBus[ 4] low8 + sBus[ 3] high2
	ch[3] = ((data[6] & 0x0F) << 7) + (data[5] >> 1); //sBus[ 6] low4 + sBus[ 5] high7
	ch[4] = ((data[7] & 0x7F) << 4) + (data[6] >> 4); //sBus[ 7] low7 + sBus[ 6] high4
	ch[5] = ((data[9] & 0x03) << 9) + (data[8] << 1) + (data[7] >> 7); //sBus[ 9] low2 + sBus[ 8] low8 + sBus[ 7] high1
	ch[6] = ((data[10] & 0x1F) << 6) + (data[9] >> 2); //sBus[10] low5 + sBus[ 9] high6
	ch[7] = ((data[11] & 0xFF) << 3) + (data[10] >> 5); //sBus[11] low8 + sBus[10] high3

	ch[8] = ((data[13] & 0x07) << 8) + data[12]; //sBus[13] low3 + sBus[12] low8
	ch[9] = ((data[14] & 0x3F) << 5) + (data[13] >> 3); //sBus[14] low6 + sBus[13] high5
	ch[10] = ((data[16] & 0x01) << 10) + (data[15] << 2) + (data[14] >> 6); //sBus[16] low1 + sBus[15] low8 + sBus[14] high2
	ch[11] = ((data[17] & 0x0F) << 7) + (data[16] >> 1); //sBus[17] low4 + sBus[16] high7
	ch[12] = ((data[18] & 0x7F) << 4) + (data[17] >> 4); //sBus[18] low7 + sBus[17] high4
	ch[13] = ((data[20] & 0x03) << 9) + (data[19] << 1) + (data[18] >> 7); //sBus[20] low2 + sBus[19] low8 + sBus[18] high1
	ch[14] = ((data[21] & 0x1F) << 6) + (data[20] >> 2); //sBus[21] low5 + sBus[20] high6
	ch[15] = ((data[22] & 0xFF) << 3) + (data[21] >> 5); //sBus[22] low8 + sBus[21] high3

	return 0;
}

uint8_t sbus_ch2pwm(uint16_t *ch, uint16_t *pwm_val)
{

	for (uint16_t i = 0; i < 16; i++) {
		pwm_val[i] = PWM_LOW
				+ (ch[i] - SBUS_CH_LOW) / (SBUS_CH_HIGH - SBUS_CH_LOW)
						* (PWM_HIGH - PWM_LOW);
		if (pwm_val[i] <= PWM_LOW)
			pwm_val[i] = PWM_LOW;
		if (pwm_val[i] >= PWM_HIGH)
			pwm_val[i] = PWM_HIGH;

	}
	return 0;
//#define PWM_LOW				1000.000
//#define PWM_HIGH			2000.000
//#define SBUS_CH_LOW			300.000
//#define SBUS_CH_HIGH		1700.000

}
